# Loading required packages
library(ggplot2); library(stargazer)
# Loading the data
load("analysis_1_data.RData")

#############
# TABLE B.1 #
#############
# Subsetting to just Scalia vacancy survey
scalia.full <- nominees[which(nominees$scalia_vacancy == 1),]
# NA or blank ("") responses will be omitted from sample proportions apart from PID

# Gender - 1 is male, 0 is female
round(prop.table(table(scalia.full$male)),3)

# Race - 1 is American Indian, 2 is Asian, 3 is Black, 4 is White, 5 is Other
round(prop.table(table(scalia.full$scalia_race)),3)

# Partisanship (treating leaners as partisans)
round(prop.table(table(scalia.full$democrat)),3)
round(prop.table(table(scalia.full$republican)),3)
round(prop.table(table(scalia.full$independent)),3)

# Education - 1 is no HS, 2 is some HS, 3 is HS, 4 is vocational school, 5 is some college, 6 is associates, 7 is college degree, 8 is some after college, 9 is postgrad degree
round(prop.table(table(scalia.full$education)),3)

# Income - 1 is under 20K, 2 is 20-35, 3 is 35-50, 4 is 50-75, 5, is 75-100, 6 is 100+
round(prop.table((table(scalia.full$scalia_income))),3)
sum(round(prop.table(table(scalia.full$scalia_income)),3)[2])
sum(round(prop.table(table(scalia.full$scalia_income)),3)[3:4])
sum(round(prop.table(table(scalia.full$scalia_income)),3)[5])
sum(round(prop.table(table(scalia.full$scalia_income)),3)[6])
sum(round(prop.table(table(scalia.full$scalia_income)),3)[7])

# Ideology, (for Scalia: 1 liberal, 2 conservative, 3 moderate, treating 8 (don't know) as don't know)
round(prop.table(table(scalia.full$scalia_ideology)),3)

#############
# TABLE B.2 #
#############
# Subsetting to just Kennedy vacancy survey
kennedy.full <- nominees[which(nominees$scalia_vacancy == 0),]

# Gender - 1 is male, 0 is female
round(prop.table(table(kennedy.full$male)),3)

# Race - 1 is White, 2 is Black, 3 is Hispanic, 4 is Asian, 5 is Other
round(prop.table(table(kennedy.full$kennedy_race)),3)

# Partisanship (treating leaners as partisans)
round(prop.table(table(kennedy.full$democrat)),3)
round(prop.table(table(kennedy.full$republican)),3)
round(prop.table(table(kennedy.full$independent)),3)

# Education - 1 is no HS, 2 is some HS, 3 is HS, 4 is job specific training, 5 is some college, 6 is associates, 7 is college degree, 8 is some after college, 9 is postgrad degree
round(prop.table(table(kennedy.full$education)),3)

# Income - 1 is under 15K, 2 is 15-25, 3 is 25-35, 4 is 35-50, 5 is 50-75, 6 is 75-100, 7 is 100-125, 8 is 125-150, 9-11 is 150+, 99 is prefer not to say
round(prop.table(table(kennedy.full$kennedy_income)),3)
sum(round(prop.table(table(kennedy.full$kennedy_income)),3)[1:2])
sum(round(prop.table(table(kennedy.full$kennedy_income)),3)[3:4])
sum(round(prop.table(table(kennedy.full$kennedy_income)),3)[5])
sum(round(prop.table(table(kennedy.full$kennedy_income)),3)[6])
sum(round(prop.table(table(kennedy.full$kennedy_income)),3)[7])
sum(round(prop.table(table(kennedy.full$kennedy_income)),3)[8])
sum(round(prop.table(table(kennedy.full$kennedy_income)),3)[9:11])
sum(round(prop.table(table(kennedy.full$kennedy_income)),3)[12])

# Ideology, for Kennedy: 1 liberal, 2 moderate, 3 conservative
round(prop.table(table(kennedy.full$kennedy_ideology)),3)

###############################################
# FIGURE B.1 AND SECTION B.4 TEXT DESCRIPTION #
###############################################
# Slimming datasets to just partisans
scalia.partisans <- scalia.full[which(scalia.full$independent == 0),]
kennedy.partisans <- kennedy.full[which(kennedy.full$independent == 0),]
nominees.partisans <- nominees[which(nominees$independent == 0),]

### Correlation of support for delay and party aligned with president ###
# Scalia
round(cor(scalia.partisans$support_delay, scalia.partisans$party_aligned_with_president, use="pairwise.complete.obs"),3)
# Kennedy
round(cor(kennedy.partisans$support_delay, kennedy.partisans$party_aligned_with_president, use="pairwise.complete.obs"),3)

# Correlation of support for delay and ideology aligned with president
# Scalia
round(cor(scalia.partisans$support_delay, scalia.partisans$ideology_aligned_with_president, use="pairwise.complete.obs"),3)
# Kennedy
round(cor(kennedy.partisans$support_delay, kennedy.partisans$ideology_aligned_with_president, use="pairwise.complete.obs"),3)

### Mean support for obstruction by partisan groups (not dropping independents) ###
# Scalia
# Overall
round(mean(scalia.full$support_delay, na.rm=T),3)*100

# Co-partisans of president (including leaners)
round(mean(scalia.full$support_delay[which(scalia.full$party_aligned_with_president == 1)], na.rm=T),3)*100

# Out-partisans of president (including leaners)
round(mean(scalia.full$support_delay[which(scalia.full$outpartisan_of_president == 1)], na.rm=T),3)*100

# Independents (true independents)
round(mean(scalia.full$support_delay[which(scalia.full$independent == 1)], na.rm=T),3)*100

# Kennedy
# Overall
round(mean(kennedy.full$support_delay, na.rm=T),3)*100

# Co-partisans of president (including leaners)
round(mean(kennedy.full$support_delay[which(kennedy.full$party_aligned_with_president == 1)], na.rm=T),3)*100

# Out-partisans of president (including leaners)
round(mean(kennedy.full$support_delay[which(kennedy.full$outpartisan_of_president == 1)], na.rm=T),3)*100

# Independents (true independents)
round(mean(kennedy.full$support_delay[which(kennedy.full$independent == 1)], na.rm=T),3)*100

### Two-way table of numbers and percentages for support for obstruction and alignment with president (dropping independents) ###
# Scalia
table(scalia.partisans$support_delay, scalia.partisans$party_aligned_with_president)
round(prop.table(table(scalia.partisans$support_delay, scalia.partisans$party_aligned_with_president)),3)

# Kennedy
table(kennedy.partisans$support_delay, kennedy.partisans$party_aligned_with_president)
round(prop.table(table(kennedy.partisans$support_delay, kennedy.partisans$party_aligned_with_president)),3)

### Plotting pairwise relationship, loess lines, jittered dots, text for correlation and two-way table raw numbers and percentages ###
set.seed(100)
corrplot.data <- nominees
colors <- ifelse(corrplot.data$vacancy == "Scalia", "darkred","blue2")
shapes <- ifelse(corrplot.data$vacancy == "Scalia", 16,1)

ggplot(corrplot.data, aes(x=party_aligned_with_president,y=support_delay)) + 
  xlab("Copartisan of President") +
  ylab("Support Delay") +
  geom_jitter(width=.1, height=.1, color=colors, shape=shapes) + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(), axis.title = element_text(size=14),
        axis.text = element_text(size=10)) + 
  geom_smooth(method="loess",data=subset(corrplot.data, vacancy == "Kennedy"), color="blue2")+ 
  geom_smooth(method="loess",data=subset(corrplot.data, vacancy == "Scalia"), color="darkred") + 
  scale_x_continuous(breaks=seq(0,1,1),labels= c("No","Yes")) + 
  scale_y_continuous(breaks=seq(0,1,1),labels= c("No","Yes")) +
  annotate("text", x=0.65, y=0.65, label= "Scalia (Correlation = -0.579)", color="darkred") +
  annotate("text", x=0.35, y=0.35, label= "Kennedy (Correlation = -0.459)", color="blue2") +
  # Copartisan no, support no
  annotate("text", x=0.28, y=-0.02, label= "Kennedy (n=177, 15.2%)", color="blue2") +
  annotate("text", x=0.28, y=-0.06, label= "Scalia (n=112, 8.1%)", color="darkred") +
  # Copartisan no, support yes
  annotate("text", x=0.28, y=1.02, label= "Kennedy (n=443, 38.0%)", color="blue2") +
  annotate("text", x=0.28, y=0.98, label= "Scalia (n=498, 35.8%)", color="darkred") +
  # Copartisan yes, support no
  annotate("text", x=0.72, y=-0.02, label= "Kennedy (n=407, 34.9%)", color="blue2") +
  annotate("text", x=0.72, y=-0.06, label= "Scalia (n=598, 43.0%)", color="darkred") +
  # Copartisan yes, support yes
  annotate("text", x=0.72, y=1.02, label= "Kennedy (n=139, 11.9%)", color="blue2") +
  annotate("text", x=0.72, y=0.98, label= "Scalia (n=182, 13.1%)", color="darkred")

#############
# TABLE C.1 #
#############
# Slimming to just partisans; treating leaners as independents
nominees.true.partisans <- nominees[which(nominees$independents_including_leaners == 0),]
kennedy.true.partisans <- kennedy.full[which(kennedy.full$independents_including_leaners == 0),]
scalia.true.partisans <- scalia.full[which(scalia.full$independents_including_leaners == 0),]

# Both nominations, no interactions
model.1.true.partisans <- lm(support_delay ~ party_aligned_with_president_true_partisans
                             + knowledge_of_vacancy + sc_approve + white + age_numeric + male 
                             + education + scalia_vacancy, weights=weights, data=nominees.true.partisans)

# Scalia only
model.2.true.partisans <- lm(support_delay ~ party_aligned_with_president_true_partisans
                             + knowledge_of_vacancy + sc_approve + white + age_numeric + male 
                             + education, weights=weights, data=scalia.true.partisans)

# Kennedy only
model.3.true.partisans <- lm(support_delay ~ party_aligned_with_president_true_partisans
                             + knowledge_of_vacancy + sc_approve + white + age_numeric + male 
                             + education, weights=weights, data=kennedy.true.partisans)

# Both nominations, with interactions
model.4.true.partisans <- lm(support_delay ~ party_aligned_with_president_true_partisans*sc_approve*knowledge_of_vacancy 
                             + party_aligned_with_president_true_partisans + knowledge_of_vacancy + sc_approve + white + age_numeric 
                             + male + education + scalia_vacancy, weights=weights, data=nominees.true.partisans)

appendix.models.true.partisans <- list(model.1.true.partisans, model.2.true.partisans, model.3.true.partisans, model.4.true.partisans)
stargazer(appendix.models.true.partisans, digits=2, star.cutoffs=c(0.05), no.space=T,
          covariate.labels = c("Party Alignment","Vacancy Knowledge","SC Approval",
                               "White","Age","Male","Education","Scalia Vacancy",
                               "Party Alignment $\\times$ SC Approval",
                               "Party Alignment $\\times$ Vacancy Knowledge",
                               "SC Approval $\\times$ Vacancy Knowledge",
                               "Party Alignment $\\times$ SC Approval $\\times$ Vacancy Knowledge",
                               "Constant"),
          dep.var.labels = "DV: Support for Delay",
          dep.var.caption = "",
          title="Determinants of Support for Delay in Considering Court Nominees: Leaners as Independents",
          label = "leaners-as-independents",
          keep.stat =  c("n","rsq","f"))

#############
# TABLE C.2 #
#############
model.1.4pt.approval <- lm(support_delay ~ party_aligned_with_president
                           + knowledge_of_vacancy + sc_approve_4pt + white + age_numeric + male 
                           + education + scalia_vacancy, weights=weights, data=nominees.partisans)

model.2.4pt.approval <- lm(support_delay ~ party_aligned_with_president
                           + knowledge_of_vacancy + sc_approve_4pt + white + age_numeric + male 
                           + education, weights=weights, data=scalia.partisans)

model.3.4pt.approval <- lm(support_delay ~ party_aligned_with_president
                           + knowledge_of_vacancy + sc_approve_4pt + white + age_numeric + male 
                           + education, weights=weights, data=kennedy.partisans)

model.4.4pt.approval <- lm(support_delay ~ party_aligned_with_president*sc_approve_4pt*knowledge_of_vacancy 
                           + party_aligned_with_president + knowledge_of_vacancy + sc_approve_4pt + white + age_numeric 
                           + male + education + scalia_vacancy, weights=weights, data=nominees.partisans)

appendix.4pt.models <- list(model.1.4pt.approval, model.2.4pt.approval, model.3.4pt.approval, model.4.4pt.approval)
stargazer(appendix.4pt.models, digits=2, star.cutoffs=c(0.05), no.space=T,
          covariate.labels = c("Party Alignment","Vacancy Knowledge","SC Approval (4-Point)",
                               "White","Age","Male","Education","Scalia Vacancy",
                               "Party Alignment $\\times$ SC Approval (4-Point)",
                               "Party Alignment $\\times$ Vacancy Knowledge",
                               "SC Approval (4-Point) $\\times$ Vacancy Knowledge",
                               "Party Alignment $\\times$ SC Approval (4-Point) $\\times$ Vacancy Knowledge",
                               "Constant"),
          dep.var.labels = "DV: Support for Delay",
          dep.var.caption = "",
          title="Determinants of Support for Delay in Considering Court Nominees: Four-Point Court Approval Measure",
          label = "four-point-approval",
          keep.stat = c("n","rsq","f"))

#############
# TABLE C.3 #
#############
# Model 1: both nominations, no interactions
model.1 <- lm(support_delay ~ party_aligned_with_president
              + sc_approve + knowledge_of_vacancy + white + age_numeric + male 
              + education + scalia_vacancy, weights=weights, data=nominees.partisans)

# Model 2: Scalia only
model.2.scalia <- lm(support_delay ~ party_aligned_with_president
                     + sc_approve + knowledge_of_vacancy + white + age_numeric + male 
                     + education, weights=weights, data=scalia.partisans)

# Model 3: Kennedy only
model.3.kennedy <- lm(support_delay ~ party_aligned_with_president
                      + sc_approve + knowledge_of_vacancy + white + age_numeric + male 
                      + education, weights=weights, data=kennedy.partisans)

# Model 4: Both nominations, triple interaction
model.4.triple.interaction <- lm(support_delay ~ party_aligned_with_president*sc_approve*knowledge_of_vacancy 
                                 + party_aligned_with_president + knowledge_of_vacancy + sc_approve + white + age_numeric 
                                 + male + education + scalia_vacancy, weights=weights, data=nominees.partisans)

table.models <- list(model.1, model.2.scalia, model.3.kennedy, model.4.triple.interaction)
stargazer(table.models, digits=2, star.cutoffs=c(0.05), no.space=T,
          covariate.labels = c("Party Alignment","SC Approval","Vacancy Knowledge",
                               "White","Age","Male","Education","Scalia Vacancy",
                               "Party Alignment $\\times$ SC Approval",
                               "Party Alignment $\\times$ Vacancy Knowledge",
                               "SC Approval $\\times$ Vacancy Knowledge",
                               "Party Alignment $\\times$ SC Approval $\\times$ Vacancy Knowledge",
                               "Constant"),
          dep.var.labels = "DV: Support for Delay",
          dep.var.caption = "",
          title="Determinants of Support for Delay in Considering Court Nominees: Full Regression Results",
          label = "ols-support-for-delay",
          keep.stat = c("n","rsq","f"))

#############
# TABLE C.4 #
#############
model.1.logit <- glm(support_delay ~ party_aligned_with_president
                     + knowledge_of_vacancy + sc_approve + white + age_numeric + male 
                     + education + scalia_vacancy,
                     weights=weights, data=nominees.partisans, family="binomial"(link="logit"), x=T)

model.2.logit <- glm(support_delay ~ party_aligned_with_president
                     + knowledge_of_vacancy + sc_approve + white + age_numeric + male 
                     + education,
                     weights=weights, data=scalia.partisans, family="binomial"(link="logit"), x=T)

model.3.logit <- glm(support_delay ~ party_aligned_with_president
                     + knowledge_of_vacancy + sc_approve + white + age_numeric + male 
                     + education,
                     weights=weights, data=kennedy.partisans, family="binomial"(link="logit"), x=T)

model.4.logit <- glm(support_delay ~ party_aligned_with_president*sc_approve*knowledge_of_vacancy 
                     + party_aligned_with_president + knowledge_of_vacancy + sc_approve + white + age_numeric 
                     + male + education + scalia_vacancy,
                     weights=weights, data=nominees.partisans, family="binomial"(link="logit"), x=T)

appendix.logistic.models <- list(model.1.logit, model.2.logit, model.3.logit, model.4.logit)
stargazer(appendix.logistic.models, digits=2, star.cutoffs=c(0.05), no.space=T,
          covariate.labels = c("Party Alignment","Vacancy Knowledge","SC Approval",
                               "White","Age","Male","Education","Scalia Vacancy",
                               "Party Alignment $\\times$ SC Approval",
                               "Party Alignment $\\times$ Vacancy Knowledge",
                               "SC Approval $\\times$ Vacancy Knowledge",
                               "Party Alignment $\\times$ SC Approval $\\times$ Vacancy Knowledge",
                               "Constant"),
          dep.var.labels = "DV: Support for Delay",
          dep.var.caption = "",
          title="Determinants of Support for Delay in Considering Court Nominees: Logistic Regression Models",
          label = "logistic-delay",
          keep.stat = c("n","AIC","ll"))

#############
# TABLE C.5 #
#############
# Model 1 with the interaction between party alignment and nomination context
model.1.context.interaction <- lm(support_delay ~ knowledge_of_vacancy + sc_approve + white + age_numeric + male 
                                   + education + scalia_vacancy*party_aligned_with_president + scalia_vacancy + party_aligned_with_president,
                                   weights=weights, 
                                   data=nominees.partisans)

stargazer(model.1.context.interaction, digits=2, star.cutoffs=c(0.05), no.space=T,
          order = c(8,2,1,7,9),
          covariate.labels = c("Party Alignment","SC Approval","Vacancy Knowledge","Scalia Vacancy",
                               "Scalia Vacancy $\\times$ Party Alignment","White","Age","Male","Education","Constant"),
          dep.var.labels = "DV: Support for Delay",
          dep.var.caption = "",
          title="Determinants of Support for Delay in Considering Court Nominees: Contextual Effect of Partisan Alignment",
          label = "analysis-1-ideological-context",
          keep.stat = c("n","rsq","f"))

#############
# TABLE C.6 #
#############
# Separate model for SC approval and interaction
approval.model <- lm(support_delay ~ party_aligned_with_president*sc_approve + party_aligned_with_president
                        + knowledge_of_vacancy + sc_approve + white + age_numeric 
                        + male + education + scalia_vacancy, weights=weights, data=nominees.partisans)

# Separate model for knowledge of vacancy and interaction
knowledge.model <- lm(support_delay ~ party_aligned_with_president*knowledge_of_vacancy + party_aligned_with_president
                         + knowledge_of_vacancy + sc_approve + white + age_numeric 
                         + male + education + scalia_vacancy, weights=weights, data=nominees.partisans)

approval.knowledge.models <- list(approval.model, knowledge.model)
# Stargazer table
stargazer(approval.knowledge.models, digits=2, star.cutoffs=c(0.05), no.space=T,
          covariate.labels = c("Party Alignment","SC Approval","Vacancy Knowledge",
                               "White","Age","Male","Education","Scalia Vacancy",
                               "Party Alignment $\\times$ SC Approval",
                               "Party Alignment $\\times$ Vacancy Knowledge",
                               "Constant"),
          dep.var.labels = "DV: Support for Delay",
          dep.var.caption = "",
          title="Determinants of Support for Delay in Considering Court Nominees: Separate Regressions for Interactions",
          label = "ols-support-for-delay-separate-regressions-interactions",
          keep.stat = c("n","rsq","f"))
